package user

import (
	"APi/client"
	"APi/request"
	"APi/response"
	"common/globals"
	"common/pkg"
	"github.com/gin-gonic/gin"
	user "user_srv/proto"
)

func LoginUser(c *gin.Context) {
	var data request.LoginUserRequest
	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, 101, err.Error())
		return
	}
	res, err := client.UserLogin(c, &user.UserLoginRequest{
		Tel:    data.Tel,
		Passwd: data.Password,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, err.Error())
		return
	}
	token, err := pkg.NewJWT(globals.JWTSecret).CreateToken(pkg.CustomClaims{ID: uint(res.UserId)})
	if err != nil {
		response.CurrencyErrResponse(c, 401, "token 生成失败"+err.Error())
		return
	}
	response.CurrencySuccessResponse(c, "用户登录成功", map[string]interface{}{"token": token})
}
